<template>
  <div class="base-echart"></div>
  <div ref="echartDivRef" :style="{ width: width, height: height }"></div>
</template>

<script lang="ts" setup>
import { ref, onMounted, defineProps, withDefaults, watchEffect } from 'vue'
import { EChartsOption } from 'echarts'
import useEchart from '@/base-ui/echart/hooks/useEchart'

//定义props,通过定义泛型来设置prop的数据类型
const props = withDefaults(
  defineProps<{
    options: EChartsOption
    width?: string
    height?: string
  }>(),
  {
    width: '100%',
    height: '360px'
  }
)
const echartDivRef = ref<HTMLElement>()
onMounted(() => {
  const { setOptions } = useEchart(echartDivRef.value!)
  //因为options数据已经发生改变，onMounted只会执行一次，所以使用watchEffect
  //因此需要搜集函数体和依赖，当发生改变时，重新执行这个函数
  watchEffect(() => {
    setOptions(props.options)
  })
})
</script>

<style scoped></style>
